<# 
.SYNOPSIS 
	Connect to FloodLight controller
#>
Function Connect-FLcontroller {
	[CmdletBinding()]
	param (
		[Parameter(Mandatory=$True,Position=1)][string]$ControllerIP,
		[Parameter(Mandatory=$True,Position=2)][string]$ControllerPort,
		[Parameter(Mandatory=$True,Position=3)][string]$ConnectionProtocol
	)
	$global:FLControllerURL = "$ConnectionProtocol`://$ControllerIP`:$ControllerPort"
	$url = "$FLControllerURL/wm/core/health/json"
	Try {
		$JsonAnswer = Invoke-WebRequest $url
	} Catch {
		Remove-Variable FLControllerURL -Scope Global
		Write-Error "Unable to connect to the remote server"
	}
}

<# 
.SYNOPSIS 
	Retrieve aggregate stats from one or all switches
#>
Function Get-FLstats {
	[CmdletBinding()]
	param (
		[Parameter(Mandatory=$True,Position=1)]
		[ValidateSet("port","queue","flow","aggregate","desc","table","features","host")] 
		[string]$statType,
		[Parameter(Mandatory=$False,Position=2)][string]$switchId = "all"
	)
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/wm/core/switch/$switchId/$statType/json" 
	$JsonAnswer = Invoke-WebRequest $url
	$Answer = $JsonAnswer.Content | ConvertFrom-Json
	Return $Answer
}

<# 
.SYNOPSIS 
	List of all switch DPIDs connected to the controller
#>
Function Get-FLswitches {
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/wm/core/controller/switches/json" 
	$JsonAnswer = Invoke-WebRequest $url
	$Answer = $JsonAnswer.Content | ConvertFrom-Json
	Return $Answer
}

<# 
.SYNOPSIS 
	Controller summary (# of Switches, # of Links, etc)
#>
Function Get-FLsummary {
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/wm/core/controller/summary/json" 
	$JsonAnswer = Invoke-WebRequest $url
	$Answer = $JsonAnswer.Content | ConvertFrom-Json
	Return $Answer
}

<# 
.SYNOPSIS 
	List of global traffic counters in the controller (across all switches)
#>
Function Get-FLcounters {
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/wm/core/counter/all/json" 
	$JsonAnswer = Invoke-WebRequest $url
	$Answer = $JsonAnswer.Content | ConvertFrom-Json
	Return $Answer
}

<# 
.SYNOPSIS 
	List of all devices tracked by the controller. This includes MACs, IPs, and attachment points.
#>
Function Get-FLdevices {
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/wm/device/" 
	$JsonAnswer = Invoke-WebRequest $url
	$Answer = $JsonAnswer.Content | ConvertFrom-Json
	Return $Answer
}